﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly HallDbContext _DB;

        public UserController(HallDbContext context)
        {
            _DB =context;
        }
        [HttpGet]
        public List<EntityUser> GetAllUser()
        {
            return _DB.EntityUser.ToList();
        }

        /// <summary>
        /// 排序
        /// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public List<EntityUser> GetAll()
        //{
        //    return _DB.EntityUser.OrderByDescending(s => s.Id).ToList();
        //}



        //[HttpGet]


        //public List<EntityUser> GetAll([FromHeader] string firstName, [FromHeader] int age)
        //{
        //    IQueryable<EntityUser> query = _DB.EntityUser;
        //    string a = "";
        //    a += "a";

        //    if (!string.IsNullOrEmpty(firstName))
        //    {
        //        query = query.Where(s => s.FirstName == firstName);
        //    }
        //    if(age>18)
        //    {
        //        query = query.Where(s => s.Age == age);
        //    }

        //    return query.ToList();
        //}///有错

    //public List<EntityUser> GetEntityUsers([FromBody] string FirstName, [FromBody] int Age)
    //{
    // List<EntityUser> users = new List<EntityUser>();
    //if(!string.IsNullOrEmpty(FirstName))
    //{
    //    users = _DB.EntityUser.Where(s => s.FirstName == FirstName).ToList();
    //}
     //if(Age>18)
     //       {
     //           users = _DB.EntityUser.Where(s => s.Age == Age).Tolist();
     //       }

    //return users;
//}




//public string? GetAllUser()
//{
//    return _DB.EntityUser.Where(w => w.FirstName == "张").Select(s => s.FirstName).FirstOrDefault();
//}


//public Result GetAllUser()
//  {
//      var UserName = _DB.EntityUser.Where(w => w.FirstName == "张").Select(s => new
//      {
//          s.FirstName,
//          s.LastNanme
//      }).FirstOrDefault();

//      Result result = new Result();
//      result.Data = UserName;
//      result.Code = 200;
//      return result;

//  }



//  [HttpGet("{Id}")]
//  public Result Get(Guid Id)
//  {
//      Result result = new Result();
//      EntityUser? resultData = _DB.EntityUser.Where(w => w.Id == Id).FirstOrDefault<EntityUser>();
//      if (resultData == null)
//      {
//          result.Code = 404;
//      }
//      else
//      {
//          result.Code = 200;
//          result.Data = resultData;
//      }
//      return result;
//  }



/// <summary>
/// 添加用户信息
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
[HttpPost]

        public Result AddUser([FromBody] EntityUser user)
        {
            Result result = new Result();
            _DB.EntityUser.Add(user);
            //SaveChanges:返回值为影响的行数
            //如果值为0，表示 没有任何数据被修改
            //大于0 表示至少有1行被修改
            result.Data=_DB.SaveChanges();

            return result;
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{Id}")]
        public Result DelUser(Guid id)
        {
            Result result = new Result();

            EntityUser? user = _DB.EntityUser.Where(w => w.Id == id).SingleOrDefault();
            if (user != null)
                _DB.EntityUser.Remove(user);
            result.Data = _DB.SaveChanges();
            return result;
        }


        /// <summary>
        /// 改用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public Result UpUser([FromBody] EntityUser user)
        {
            Result result = new Result();
            _DB.EntityUser.Update(user);
            result.Data = _DB.SaveChanges();

            return result;
        }

        
    }
}
